package cmsc128.proj.iterc.dao;

import java.util.ArrayList;
import java.util.List;
import org.apache.torque.util.Criteria;
/**
 *
 * @author Lean
 */
public class BookPeer extends BaseBookPeer {


    public static boolean addBook(String isbn, String status, String publisher, String title, String author, String year_published, String date_acquired){
        
        if(isbn.isEmpty() || status.isEmpty() || publisher.isEmpty() || title.isEmpty() || author.isEmpty())
        {
            return false;
        }

        Criteria criteria = new Criteria();

        java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");

        try{
            //java.util.Date yp = df.parse(year_published);
            //criteria.add(BookPeer.YEAR_PUBLISHED, yp);
            java.util.Date da = df.parse(date_acquired);                    
            criteria.add(BookPeer.DATE_ACQUIRED, da);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        criteria.add(BookPeer.ISBN, isbn);
        criteria.add(BookPeer.STATUS, status);
        criteria.add(BookPeer.PUBLISHER, publisher);
        criteria.add(BookPeer.TITLE, title);
        criteria.add(BookPeer.AUTHOR, author);
        criteria.add(BookPeer.YEAR_PUBLISHED, year_published);
        criteria.add(BookPeer.ACQUIRED, true);

        try{
            BookPeer.doInsert(criteria);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public static List<Book> viewBooks(){
        Criteria criteria = new Criteria();
        criteria.add(BookPeer.ACQUIRED, true);

        try{
            return (List<Book>)BookPeer.doSelect(criteria);
        }catch(Exception e){
            e.printStackTrace();
            return new ArrayList<Book>();
        }
    }

        public static List<Book> viewAvailableBooks(){
        Criteria criteria = new Criteria();
        criteria.add(BookPeer.STATUS, "available");

        try{
            return (List<Book>)BookPeer.doSelect(criteria);
        }catch(Exception e){
            e.printStackTrace();
            return new ArrayList<Book>();
        }
    }

        public static List<Book> viewBorrowedBooks(){
        Criteria criteria = new Criteria();
        criteria.add(BookPeer.STATUS, "borrowed");

        try{
            return (List<Book>)BookPeer.doSelect(criteria);
        }catch(Exception e){
            e.printStackTrace();
            return new ArrayList<Book>();
        }
    }

        public static List<Book> viewDueBooks(){
        Criteria criteria = new Criteria();
        criteria.add(BookPeer.STATUS, "due");

        try{
            return (List<Book>)BookPeer.doSelect(criteria);
        }catch(Exception e){
            e.printStackTrace();
            return new ArrayList<Book>();
        }
    }
    
   public static List<Book> searchBook(String search){
      if(search.isEmpty())
        {
            return null;
        }
       Criteria criteria  = new Criteria();
      // Criteria criteria2  = new Criteria();

       Criteria.Criterion c1 = criteria.getNewCriterion(BookPeer.AUTHOR, "%" + search + "%", Criteria.LIKE);
       c1.or(criteria.getNewCriterion(BookPeer.TITLE, "%" + search + "%", Criteria.LIKE));
       //Criteria.Criterion c2 = criteria.getNewCriterion(BookPeer.TITLE, "%" + search + "%", Criteria.LIKE);
       //c1.or(c2);
      
       criteria.add(c1);
 
       try{
            return (List<Book>)BookPeer.doSelect(criteria);
        }catch(Exception e){
            e.printStackTrace();
            try{
                 return (List<Book>)BookPeer.doSelect(criteria);
            }catch(Exception f){
                 f.printStackTrace();
                 return null;
            }
          }


    }

    public static boolean updateBook(int id, String isbn, String status, String publisher, String title, String author, String year_published, String date_acquired){
        
        if(isbn.isEmpty() || status.isEmpty() || publisher.isEmpty() || title.isEmpty() || author.isEmpty())
        {
            return false;
        }

        Criteria selectCriteria = new Criteria();
        selectCriteria.add(BookPeer.ID, id);

        Criteria criteria = new Criteria();

        java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");

        try{
           // java.util.Date yp = df.parse(year_published);
           // criteria.add(BookPeer.YEAR_PUBLISHED, yp);
            java.util.Date da = df.parse(date_acquired);
            criteria.add(BookPeer.DATE_ACQUIRED, da);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        criteria.add(BookPeer.ISBN, isbn);
        criteria.add(BookPeer.STATUS, status);
        criteria.add(BookPeer.PUBLISHER, publisher);
        criteria.add(BookPeer.TITLE, title);
        criteria.add(BookPeer.AUTHOR, author);
        criteria.add(BookPeer.YEAR_PUBLISHED, year_published);
        criteria.add(BookPeer.ACQUIRED, true);

        try{
            BookPeer.doUpdate(selectCriteria, criteria);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public static boolean deleteBook(int id){
        Criteria criteria = new Criteria();
        criteria.add(BookPeer.ID, id);
        try{
            BookPeer.doDelete(criteria);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

}
